package com.app.controller;


import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.service.AdminUserService;
import com.app.service.IDividedIntoRecordService;
import com.app.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2024-05-30
 */
@RestController
@RequestMapping("/dividedIntoRecord")
public class DividedIntoRecordController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private IDividedIntoRecordService dividedIntoRecordService;

    /**
     * 第页
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param page      第页
     * @param limit     限制
     * @param areaId    区域id
     * @param request   请求
     * @return {@link Result}
     */
    @GetMapping("/page")
    public Result page(@RequestParam(required = false) String beginTime,
                       @RequestParam(required = false) String keyword,
                       @RequestParam(required = false) String endTime,
                       Integer page, Integer limit,
                       @RequestParam(required = false) Integer type,
                       @RequestParam(required = false) String areaId,
                       HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(userId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        String cityId = "";
        if (adminUser.getRoleId() == 3) {
            cityId = adminUser.getAreaId().toString();
        }
        if (adminUser.getRoleId() == 4) {
            areaId = adminUser.getAreaId().toString();
        }
        return Result.success(dividedIntoRecordService.pages(beginTime, endTime, page, limit, areaId, cityId, keyword,type));
    }


    @GetMapping("/dataStatistics")
    public Result dataStatistics(@RequestParam(required = false) String beginTime,
                                 @RequestParam(required = false) String keyword,
                                 @RequestParam(required = false) String endTime,
                                 @RequestParam(required = false) String areaId,
                                 @RequestParam(required = false) Integer type,
                                 HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        AdminUserModel adminUser = adminUserService.getById(Integer.parseInt(userId));
        if (adminUser == null) {
            return Result.error(405, "用户不存在");
        }

        String cityId = "";
        if (adminUser.getRoleId() == 3) {
            cityId = adminUser.getAreaId().toString();
        }
        if (adminUser.getRoleId() == 4) {
            areaId = adminUser.getAreaId().toString();
        }
        return Result.success(dividedIntoRecordService.dataStatistics(beginTime, endTime, areaId, cityId, keyword,type));
    }


}

